package cn.logicalthing.demo.web.controller;

import com.core.bean.User;
import com.core.service.LoginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class CustomController extends ControllerBase{

    public static final  String RESPONSE_LOGIN_TOKEN = "TOKEN";
    public static final String CUSTOM_LOGIN_RETURN = "CUSTOM_LOGIN_RETURN";
    public static final String CUSTOM_LOGIN_USER = "CUSTOM_LOGIN_USER";
    @PostMapping("/login")
    public Map<String,Object> login(String user, String pwd, String os, String ip, String devInfo, HttpServletRequest request){
        LoginService loginService = new LoginService();
        User usr = new User();
        usr.setName(user);
        Map<String,Object> result = new HashMap<>();
        try {
           String token =  loginService.doLogin(usr, pwd, request.getRemoteAddr(), devInfo, os);
           result.put(RESPONSE_LOGIN_TOKEN,token);
           result.put(RESPONSE_CODE,SUCCESS_CODE_200);
        }catch (Exception e){
            result.put(RESPONSE_CODE,ERROR_CODE_500);
            result.put(RESPONSE_MSG,e.getMessage());
            e.printStackTrace();
        }
        request.setAttribute(CUSTOM_LOGIN_RETURN,result);
        request.setAttribute(CUSTOM_LOGIN_USER,user);
        return result;
    }

}
